package com.example.springcloud.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.example.springcloud.entity.Dept;

@RestController
public class DeptController_consumer {

	Logger logger = LoggerFactory.getLogger(getClass());
	
	//private static final String REST_URL_PREFIX = "http://localhost:8081"; //不使用微服务架构直接用RestTemplate
	private static final String REST_URL_PREFIX = "http://microservicecloud-dept";
	@Autowired
	private RestTemplate restTemplate;
	
	
	@GetMapping("consumer/dept/get/{deptno}")
	public Dept get(@PathVariable("deptno") Long deptno) {
		logger.debug("url : "+REST_URL_PREFIX+"dept/get/"+deptno);
		return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+deptno, Dept.class);
	}
	
	@SuppressWarnings("unchecked")
	@GetMapping("consumer/dept/list")
	public List<Dept> list() {
		logger.debug("url : "+REST_URL_PREFIX+"dept/list");
		return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list", List.class);
	}
	
	
	@PostMapping("consumer/dept/add")
	public boolean add(Dept dept) {
		logger.debug("url : "+REST_URL_PREFIX+"dept/add");
		return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add", dept, Boolean.class);
	}
	
	
}
